<script setup>

import {getCurrentInstance, ref} from "vue";
import {deleteSysNotice, listSysNotice} from "@/api/system/notice";
import {noticeStatusList, noticeTypeList} from "@/enum/enum";
import SysNoticeForm from "@/views/system/notice/SysNoticeForm.vue";
import {convertSortParam, enumToArray} from "@/utils/utils";

const {proxy} = getCurrentInstance();
const sysNoticeList = ref([]);
const total = ref(0);
const sysNoticeFormRef = ref();
const queryFormRef = ref();
const queryForm = ref({
  pageNo: 1,
  pageSize: 10,
  noticeTitle: null,
  noticeType: null,
  status: null,
  timeArr: null
})
const selectIdIndex = ref(-1);
const selectId = ref();
const noticeTypeOptions = enumToArray(noticeTypeList);
const noticeStatusOptions = enumToArray(noticeStatusList);
const sortFields = ref([]);

const getList = async () => {
  if (sortFields.value.length > 0) {
    queryForm.value.sort = convertSortParam(sortFields.value);
  }
  const {data} = await listSysNotice(queryForm.value)
  total.value = data.total;
  sysNoticeList.value = data.list;
}

const openNotice = (noticeId, type) => {
  sysNoticeFormRef.value.open(noticeId, type)
}

const deleteNotice = async (noticeId) => {
  proxy.$modal.confirm('确定删除该记录吗？').then(async () => {
    await deleteSysNotice(noticeId);
    proxy.$modal.okNotify('删除成功');
    await getList();
  })
}

const handleQuery = () => {
  queryForm.value.pageNo = 1;
  getList();
}

const handleReset = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

const handleSelectionChange = (index, row) => {
  selectIdIndex.value = index;
  selectId.value = row.noticeId
}

const handleUpdate = () => {
  if (!selectId.value) {
    proxy.$modal.warningNotify('请选择一条数据');
    return false;
  }
  openNotice(selectId.value, 'update');
}

const handleDelete = () => {
  if (!selectId.value) {
    proxy.$modal.warningNotify('请选择一条数据');
    return false;
  }
  deleteNotice(selectId.value)
}
getList();
</script>

<template>
  <div class="app-container">
    <a-form :model="queryForm" ref="queryFormRef" layout="inline" class="demo-form-inline">
      <a-form-item label="公告标题" field="noticeTitle">
        <a-input v-model="queryForm.noticeTitle" placeholder="请输入公告标题"/>
      </a-form-item>
      <a-form-item label="公告类型" field="noticeType">
        <a-select v-model="queryForm.noticeType" placeholder="请选择公告类型">
          <a-option v-for="(item, index) in noticeTypeOptions" :key="index" :label="item.label" :value="item.value"/>
        </a-select>
      </a-form-item>
      <a-form-item label="公告状态" field="status">
        <a-select v-model="queryForm.status" placeholder="请选择公告状态">
          <a-option v-for="(item, index) in noticeStatusOptions" :key="index" :label="item.label" :value="item.value"/>
        </a-select>
      </a-form-item>
      <a-form-item label="创建时间" field="timeArr">
        <a-range-picker
            v-model="queryForm.timeArr"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="handleQuery">
          <icon icon="ep-search" size="14px" color="unset" class="mr-1"/>
          查询
        </a-button>
        <a-button plain @click="handleReset" class="ml-12px">
          <icon icon="ep-refresh" size="14px" color="unset" class="mr-1"/>
          重置
        </a-button>
      </a-form-item>
    </a-form>

    <a-space class="flex mb-5">
      <a-button type="primary" plain class="flex items-center" @click="openNotice(null, 'create')"
                v-hasPermission="['system:notice:add']">
        <icon icon="ep-plus" size="14px" color="unset" class="mr-1"/>
        新增
      </a-button>

      <a-button status="success" plain class="flex items-center" @click="handleUpdate"
                v-hasPermission="['system:notice:edit']">
        <icon icon="ep-edit" size="14px" color="unset" class="mr-1"/>
        编辑
      </a-button>

      <a-button status="danger" plain class="flex items-center" @click="handleDelete"
                v-hasPermission="['system:notice:delete']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        删除
      </a-button>
    </a-space>

    <a-table :data="sysNoticeList" :border="true" size="large">
      <template #columns>
        <a-table-column :width="55">
          <template #cell="{record,index}">
            <a-radio v-model="selectIdIndex" :value="index"
                     @change="handleSelectionChange(index,record)"/>
          </template>
        </a-table-column>
        <a-table-column data-index="noticeId" title="主键ID"/>
        <a-table-column data-index="noticeTitle" title="公告标题"/>
        <a-table-column data-index="noticeType" title="公告类型">
          <template #cell="{record}">
            <a-tag :color="noticeTypeList[record.noticeType].type">{{ noticeTypeList[record.noticeType].label }}
            </a-tag>
          </template>
        </a-table-column>
        <a-table-column data-index="status" title="公告状态">
          <template #cell="{record}">
            <a-tag :color="noticeStatusList[record.status].type">{{ noticeStatusList[record.status].label }}
            </a-tag>
          </template>
        </a-table-column>
        <a-table-column data-index="remark" title="备注"/>
        <a-table-column data-index="createTime" title="添加时间"/>
        <a-table-column fixed="right" title="操作" :width="200">
          <template #cell="{record}">
            <a-button class="ml-2" type="text" @click="openNotice(record.noticeId,'update')"
                      v-hasPermission="['system:notice:edit']">编辑
            </a-button>
            <a-button class="ml-2" status="danger" type="text" @click="deleteNotice(record.noticeId)"
                      v-hasPermission="['system:notice:delete']">
              删除
            </a-button>
          </template>
        </a-table-column>
      </template>
    </a-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryForm.pageNo"
        v-model:limit="queryForm.pageSize"
        @pagination="getList"/>

    <sys-notice-form ref="sysNoticeFormRef" @success="getList"/>
  </div>
</template>

<style scoped lang="scss">

</style>
